package com.xiaoma.tpo.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoma.tpo.dao.MkListeningDao;
import com.xiaoma.tpo.dao.MkListeningQuestionDao;
import com.xiaoma.tpo.dao.MkResultDao;
import com.xiaoma.tpo.dao.MkReadingQuestionDao;
import com.xiaoma.tpo.dao.MkSpeakingQuestionDao;
import com.xiaoma.tpo.dao.MkTpoDao;
import com.xiaoma.tpo.dao.MkReadingDao;
import com.xiaoma.tpo.dao.MkWritingQuestionDao;
import com.xiaoma.tpo.domain.MkListening;
import com.xiaoma.tpo.domain.MkListeningQuestion;
import com.xiaoma.tpo.domain.MkResult;
import com.xiaoma.tpo.domain.MkReadingQuestion;
import com.xiaoma.tpo.domain.MkSpeakingQuestion;
import com.xiaoma.tpo.domain.MkTpo;
import com.xiaoma.tpo.domain.MkReading;
import com.xiaoma.tpo.domain.MkWritingQuestion;
import com.xiaoma.tpo.dto.MkReadingParagraphVo;
import com.xiaoma.tpo.dto.MkReadingVo;

@Service
public class MockService {

    @Autowired
    private MkTpoDao tpoDao;
    
    @Autowired
    private MkReadingDao readingDao;
    
    @Autowired
    private MkListeningDao listeningDao;
    
    @Autowired
    private MkReadingQuestionDao readingQuestionDao;
    
    @Autowired
    private MkListeningQuestionDao listeningQuestionDao;
    
    @Autowired
    private MkSpeakingQuestionDao speakingQuestionDao;
    
    @Autowired
    private MkWritingQuestionDao writingQuestionDao;
    
    @Autowired
    private MkResultDao mockResultDao;
    
    public List<MkTpo> findAllTpos() {
        return tpoDao.findAll();
    }
    
    public List<MkReadingVo> findReadingsByTpoId(int tpoId) {
        List<MkReadingParagraphVo> readingParagraphs = readingDao.findByTpoId(tpoId);
        List<MkReadingVo> readingVos = new ArrayList<MkReadingVo>();
        int preReadingId = 0;
        for (MkReadingParagraphVo item : readingParagraphs) {
            if (item.getId() != preReadingId) {
                MkReadingVo vo = new MkReadingVo();
                vo.setId(item.getId());
                vo.setMkTpoId(item.getMkTpoId());
                vo.setTitle(item.getTitle());
                vo.setPassageNum(item.getPassageNum());
                vo.setCategory(item.getCategory());
                Map<Integer,String> paragraphs = new HashMap<Integer,String>();
                vo.setParagraphs(paragraphs);
                Map<Integer,String> paragraphCns = new HashMap<Integer,String>();
                vo.setParagraphCns(paragraphCns);
                readingVos.add(vo);
            }
            readingVos.get(readingVos.size() - 1).getParagraphs().put(item.getSeqNum(), item.getContent());
            readingVos.get(readingVos.size() - 1).getParagraphCns().put(item.getSeqNum(), item.getContentCn());
            preReadingId = item.getId();
        }
        return readingVos;
    }
    
    public List<MkListening> findListeningsByTpoId(int tpoId) {
        MkListening entity = new MkListening();
        entity.setMkTpoId(tpoId);
        return listeningDao.findByColumns(entity);
    }
    
    public List<MkReadingQuestion> findReadingQuestionsByTpoId(Integer tpoId) {
        MkReadingQuestion entity = new MkReadingQuestion();
        entity.setMkTpoId(tpoId);
        return readingQuestionDao.findByColumns(entity);
    }
    
    public List<MkListeningQuestion> findListeningQuestionsByTpoId(Integer tpoId) {
        return listeningQuestionDao.findByTpoId(tpoId);
    }
    
    public List<MkSpeakingQuestion> findSpeakingQuestionsByTpoId(Integer tpoId) {
        MkSpeakingQuestion entity = new MkSpeakingQuestion();
        entity.setMkTpoId(tpoId);
        return speakingQuestionDao.findByColumns(entity);
    }
    
    public List<MkWritingQuestion> findWritingQuestionsByTpoId(Integer tpoId) {
        MkWritingQuestion entity = new MkWritingQuestion();
        entity.setMkTpoId(tpoId);
        return writingQuestionDao.findByColumns(entity);
    }
    
    public MkReadingQuestion findReadingQuestionById(Integer questionId) {
        return readingQuestionDao.getById(questionId);
    }
    
    public MkListeningQuestion findListeningQuestionById(Integer questionId) {
        return listeningQuestionDao.getById(questionId);
    }
    
    public int saveMockResult(MkResult mockResult) {
        return mockResultDao.insert(mockResult);
    }
}
